import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/home/key_think_tank/key_think_tank_view.dart';
import 'package:leader_help/page/task/main/task/models/plan.dart';
import 'package:leader_help/page/task/subs/question_detail/widgets/cell_task.dart';
import 'widgets/cell.dart';
import 'package:leader_help/utils/component/dialog/drop.dart';
import 'package:leader_help/utils/component/list/section.dart';
import 'package:leader_help/utils/util.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class QuestionDetailController extends GetxController {
  QuestionDetailController();

  final state = QuestionDetailState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.mod = Get.arguments;
    state.tabTitles = [
      UsuallyModel(name: '问题简介', iconData: ProIconUtil.earth),
      UsuallyModel(name: '目标方案', iconData: ProIconUtil.aim),
      UsuallyModel(name: '任务安排', iconData: ProIconUtil.people_group),
      UsuallyModel(name: '最新进展', iconData: ProIconUtil.progress),
    ];

    _getData();
    _observer();
  }

  void _observer() {
    state.positionListener.itemPositions.addListener(() {
      var _first = state.positionListener.itemPositions.value.first;
      var _index = _first.index;
      var _realIndex = state.scrollIndexMap[_index];
      if (_realIndex != null && _index != state.tabActiveIndex) {
        state.tabActiveIndex = _realIndex;
        update();
      }
    });
  }

  void actionActive(int index) {
    // if (state.tabActiveIndex == index) {
    //   return;
    // }
    state.tabActiveIndex = index;
    var _realIndex = state.activeMap[index];
    state.scrollCtrl.jumpTo(index: _realIndex!);

    update();
  }

  void actionMakePlan() async {
    var mod = await Get.toNamed(RouterPath.make_plan, arguments: state.planMod);
    if (mod == null) {
      return;
    }
    state.planMod = mod;
    update();
  }

  void actionCell(PlanModel mod) {
    Get.to(() => const KeyThinkTankPage());
  }

  void actionDoTask() async {
    var mod = await Get.toNamed(RouterPath.plan_task);
    if (mod != null) {
      state.taskMod = mod;
      update();
    }
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  void _getData() {
    state.list = [
      SectionModel(
          index: 0,
          sectionHeader: const QuestionDetailSectionView(
            index: 0,
            title: '问题现象',
          ),
          itemBuilder: (context, index) {
            return const QuestionDetailCell(
              content: '资本要素配置品质低。',
            );
          },
          items: [1]),
      SectionModel(
          index: 1,
          sectionHeader: const QuestionDetailSectionView(
            title: '问题根源',
          ),
          itemBuilder: (context, index) {
            return const QuestionDetailCell(
              content:
                  '全省2040家有融资的企业，这些企业受到市场资本的认可，具有发展优势，如果能够加强提升这些企业资本要素配置，可以进一步激发激活整个市场主体的发展速度和质量。',
            );
          },
          items: [1]),
      SectionModel(
          index: 2,
          sectionHeader: const QuestionDetailSectionView(
            title: '问题症结',
          ),
          itemBuilder: (context, index) {
            return const QuestionDetailCell(
              content:
                  '资源有限，将2040家企业全部覆盖激活短期内难度较大，应该进一步聚焦在这批企业中最具发展潜力的企业，进行资源赋能，逐级激发，撬动整个经济市场的高质量发展。',
            );
          },
          items: [1]),
      SectionModel(
          index: 3,
          sectionHeader: const QuestionDetailSectionView(
            index: 1,
            title: '目标方案',
          ),
          itemBuilder: (context, index) {
            return const QuestionDetailAimCell();
          },
          items: [1]),
      SectionModel(
          index: 4,
          sectionHeader: const QuestionDetailSectionView(
            index: 2,
            title: '任务安排',
          ),
          itemBuilder: (context, index) {
            return const QuestionDetailTaskCell();
          },
          items: [1]),
      SectionModel(
          index: 5,
          sectionHeader: const QuestionDetailSectionView(
            index: 3,
            title: '最新进展',
          ),
          itemBuilder: (context, index) {
            return const QuestionDetailProgressCell();
          },
          items: [1]),
    ];

    state.activeMap = {
      0: 0,
      1: 6,
      2: 8,
      3: 10,
    };
    state.scrollIndexMap = {
      0: 0,
      6: 1,
      8: 2,
      10: 3,
    };
  }
}
